dataviz\figure\drawers/
drawerbarchart.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
use crate::figure::{
    canvas::{pixelcanvas::PixelCanvas, svgcanvas::SvgCanvas},
    figuretypes::groupbarchart::GroupBarChart,
    utilities::orientation::Orientation,
};
use ab_glyph::{FontRef, PxScale};
use imageproc::drawing::text_size;

use super::drawer::Drawer;

impl Drawer for GroupBarChart {
    fn draw_svg(&mut self, svg_canvas: &mut SvgCanvas) {
        let width = svg_canvas.width as f64;
        let height = svg_canvas.height as f64;
        let margin = svg_canvas.margin as f64;
        let font_size = 12.0;

        match self.orientation {
            Orientation::Vertical => {
                // Draw background
                svg_canvas.draw_rect(0.0, 0.0, width, height, "white", "black", 1.0, 1.0);

                // Draw Title
                svg_canvas.draw_title(
                    width / 2.0,
                    margin / 2.0,
                    &self.title,
                    font_size * 2.0,
                    "black",
                );

                // Calculate unique axis values
                let unique_x_values: Vec<u32> = self
                    .datasets
                    .iter()
                    .flat_map(|dataset| dataset.data.iter().map(|(x, _)| *x as u32))
                    .collect::<std::collections::BTreeSet<_>>()
                    .into_iter()
                    .collect();

                let x_count = unique_x_values.len();

                let y_max = self
                    .datasets
                    .iter()
                    .flat_map(|dataset| dataset.data.iter().map(|(_, y)| *y))
                    .fold(0.0_f64, |max, y| max.max(y));

                // Calculate scales
                let scale_x = (width - 2.0 * margin) / x_count as f64;
                let scale_y = (height - 2.0 * margin) / y_max;

                // Draw grid
                svg_canvas.draw_grid(
                    margin,
                    width - margin,
                    margin,
                    height - margin,
                    10,
                    10,
                    "lightgray",
                );

                // Draw axes
                let origin_x = margin;
                let origin_y = height - margin;

                svg_canvas.draw_line(origin_x, margin, origin_x, origin_y, "black", 2.0); // Y-axis
                svg_canvas.draw_line(origin_x, origin_y, width - margin, origin_y, "black", 2.0); // X-axis

                // Y-axis
                let num_ticks = 10;
                let mut y_axis_ticks = String::new();
                for i in 0..=num_ticks {
                    let value = 0.0 + i as f64 * (y_max - 0.0) / num_ticks as f64;
                    let y = height - margin - i as f64 * (height - 2.0 * margin) / num_ticks as f64;
                    let tick_start_x = origin_x - 5.0;
                    let tick_end_x = origin_x + 5.0;

                    y_axis_ticks.push_str(&format!(
                        "M {:.2},{:.2} L {:.2},{:.2} ",
                        tick_start_x, y, tick_end_x, y
                    ));

                    // Draw value as text (fallback to basic SVG <text>)
                    svg_canvas.elements.push(format!(
            r#"<text x="{:.2}" y="{:.2}" font-size="{:.2}" text-anchor="end" fill="black">{:.1}</text>"#,
            margin - 10.0, y + font_size * 0.3, font_size, value
        ));
                }
                svg_canvas.elements.push(format!(
                    r#"<path d="{}" stroke="black" stroke-width="1" fill="none"/>"#,
                    y_axis_ticks
                ));

                // Draw X-axis labels and grouped bars
                let group_width = scale_x * 0.8; // Width of each group of bars
                let bar_width = group_width / self.datasets.len() as f64; // Width of each bar

                for (group_index, x_label) in unique_x_values.iter().enumerate() {
                    let group_center_x = origin_x + (group_index as f64 + 0.4) * scale_x;
                    // let x = margin + group_index as f64 * (width - 2.0 * margin) / unique_x_values.len() as f64;

                    // Draw X-axis label
                    svg_canvas.draw_text(
                        group_center_x,
                        origin_y + font_size * 1.5,
                        &x_label.to_string(),
                        font_size,
                        "black",
                    );

                    // Draw bars for each dataset in the group
                    for (dataset_index, dataset) in self.datasets.iter().enumerate() {
                        if let Some(&(_, value)) = dataset
                            .data
                            .iter()
                            .find(|(x, _)| &(*x as u32).to_string() == &x_label.to_string())
                        {
                            let bar_height = value * scale_y;
                            let bar_left = group_center_x - group_width / 2.0
                                + dataset_index as f64 * bar_width;

                            // Draw bar
                            svg_canvas.elements.push(format!(
                        r#"<rect x="{:.2}" y="{:.2}" width="{:.2}" height="{:.2}" fill="rgb({},{},{})" stroke="black" stroke-width="1"/>"#,
                        bar_left,
                        origin_y - bar_height,
                        bar_width,
                        bar_height,
                        dataset.color[0],
                        dataset.color[1],
                        dataset.color[2]
                    ));
                        }
                    }
                }
            }
            Orientation::Horizontal => {
                let width = svg_canvas.width as f64;
                let height = svg_canvas.height as f64;
                let margin = svg_canvas.margin as f64;
                let font_size = 12.0;

                // Draw background
                svg_canvas.draw_rect(0.0, 0.0, width, height, "white", "black", 1.0, 1.0);

                // Draw Title
                svg_canvas.draw_title(
                    width / 2.0,
                    margin / 2.0,
                    &self.title,
                    font_size * 2.0,
                    "black",
                );

                // Calculate unique axis values
                let unique_y_values: Vec<u32> = self
                    .datasets
                    .iter()
                    .flat_map(|dataset| dataset.data.iter().map(|(y, _)| *y as u32))
                    .collect::<std::collections::BTreeSet<_>>()
                    .into_iter()
                    .collect();

                let y_count = unique_y_values.len();

                let x_max = self
                    .datasets
                    .iter()
                    .flat_map(|dataset| dataset.data.iter().map(|(_, x)| *x))
                    .fold(0.0_f64, |max, x| max.max(x));

                // Calculate scales
                let scale_y = (height - 2.0 * margin) / y_count as f64;
                let scale_x = (width - 2.0 * margin) / x_max;

                // Draw grid
                svg_canvas.draw_grid(
                    margin,
                    width - margin,
                    margin,
                    height - margin,
                    10,
                    10,
                    "lightgray",
                );

                // Draw axes
                let origin_x = margin;
                let origin_y = height - margin;

                svg_canvas.draw_line(origin_x, margin, origin_x, origin_y, "black", 2.0); // Y-axis
                svg_canvas.draw_line(origin_x, origin_y, width - margin, origin_y, "black", 2.0); // X-axis

                // Draw X-axis tick marks and labels
                let num_ticks = 10;
                let x_tick_step = x_max / num_ticks as f64;
                for i in 0..=num_ticks {
                    let value_x = i as f64 * x_tick_step;
                    let tick_x = origin_x + (value_x * scale_x);
                    let x = margin + i as f64 * (width - 2.0 * margin) / num_ticks as f64;
                    // Draw tick line
                    svg_canvas.draw_line(tick_x, origin_y, tick_x, origin_y + 5.0, "black", 1.0);

                    // Draw tick label
                    svg_canvas.draw_text(
                        x,
                        origin_y + font_size * 1.5,
                        &format!("{:.1}", value_x),
                        font_size,
                        "black",
                    );
                }

                // Draw Y-axis labels and grouped bars
                let group_height = scale_y * 0.8; // Height of each group of bars
                let bar_height = group_height / self.datasets.len() as f64; // Height of each bar

                for (group_index, y_label) in unique_y_values.iter().enumerate() {
                    let group_center_y = origin_y - (group_index as f64 + 0.5) * scale_y;

                    // Draw Y-axis label
                    svg_canvas.draw_text(
                        origin_x - font_size * 3.0,
                        group_center_y,
                        &y_label.to_string(),
                        font_size,
                        "black",
                    );

                    // Draw bars for each dataset in the group
                    for (dataset_index, dataset) in self.datasets.iter().enumerate() {
                        if let Some(&(_, value)) = dataset
                            .data
                            .iter()
                            .find(|(y, _)| &(*y as u32).to_string() == &y_label.to_string())
                        {
                            let bar_length = value * scale_x;
                            let bar_top = group_center_y - group_height / 2.0
                                + dataset_index as f64 * bar_height;

                            // Draw bar
                            svg_canvas.elements.push(format!(
                        r#"<rect x="{:.2}" y="{:.2}" width="{:.2}" height="{:.2}" fill="rgb({},{},{})" stroke="black" stroke-width="1"/>"#,
                        origin_x,
                        bar_top,
                        bar_length,
                        bar_height,
                        dataset.color[0],
                        dataset.color[1],
                        dataset.color[2]
                    ));
                        }
                    }
                }
            }
        }
        // Draw legend in the bottom-left corner
        let legend_x_start = 5.0; // Start at the very left with margin spacing
        let legend_y = height - margin / 2.0; // Move to bottom-left corner

        let mut legend_x = legend_x_start;
        let mut elements = String::new();

        for dataset in &self.datasets {
            elements.push_str(&format!(
                r#"<rect x="{:.2}" y="{:.2}" width="{:.2}" height="{:.2}" fill="rgb({},{},{})"/>"#,
                legend_x,
                legend_y,
                font_size,
                font_size,
                dataset.color[0],
                dataset.color[1],
                dataset.color[2]
            ));

            elements.push_str(&format!(
                r#"<text x="{:.2}" y="{:.2}" font-size="{:.2}" fill="black">{}</text>"#,
                legend_x + font_size * 1.3,
                legend_y + font_size - 2.0,
                font_size,
                dataset.label
            ));

            legend_x += font_size * 5.0 + dataset.label.len() as f64 * font_size * 0.6;
        }

        svg_canvas.draw_rect(
            legend_x_start - 5.0,
            legend_y - 5.0,
            legend_x - legend_x_start + 5.0,
            font_size + 10.0,
            "white",
            "black",
            0.5,
            0.5,
        );

        svg_canvas.elements.push(elements);
    }

    fn draw(&mut self, canvas: &mut PixelCanvas) {
        match self.orientation {
            Orientation::Vertical => self.draw_vertical(canvas),
            Orientation::Horizontal => self.draw_horizontal(canvas),
        }
    }

    fn draw_legend(&self, canvas: &mut PixelCanvas) {
        let font_bytes =
            std::fs::read(self.config.font_label.clone()).expect("Failed to read font file");
        let font = FontRef::try_from_slice(&font_bytes).unwrap();
        let scale = PxScale { x: 10.0, y: 10.0 }; // Font size

        let square_size = 10; // Size of the colored square
        let padding = 5; // Space between the square and text
        let line_height = 20; // Vertical space for each legend entry
        let legend_margin = canvas.margin; // Margin from the bottom of the canvas

        let mut x = canvas.margin;
        let mut y = canvas.height - legend_margin; // Legend starts from the bottom

        for dataset in &self.datasets {
            let (w, h) = text_size(scale, &font, &dataset.label);
            // Draw the square
            for dy in 0..square_size {
                for dx in 0..square_size {
                    canvas.draw_pixel(
                        x + dx,
                        y + square_size * 2 + dy + h, // Adjust to align above baseline
                        dataset.color,
                    );
                }
            }

            // Draw the label text next to the square
            let text_x: u32 = x + square_size + padding;
            canvas.draw_text(
                text_x,
                y + 2 * square_size + h,
                &dataset.label,
                dataset.color,
                &font,
                scale,
            );

            // Move to the next legend entry
            x += square_size + padding + w + padding;
            if x > canvas.width - canvas.margin {
                // If the width exceeds, wrap to the next row
                x = canvas.margin;
                y -= line_height;
            }
        }
    }
}